package com.polytech.diploma.OracleStat.Grouping;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.polytech.diploma.OracleStat.SQLStatData;

public class QueryGrouper {
	
	private Pattern lessOrEqualPattern = Pattern.compile("<=( )+*@(NUMBER|LITERAL)\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern greaterOrEqualPattern = Pattern.compile(">=( )+*\\(+*@(NUMBER|LITERAL)\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern strEqualPattern = Pattern.compile("=( )+*\\(+*@LITERAL\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern strNotEqualPattern = Pattern.compile("<>( )+*\\(+*@LITERAL\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern numEqualPattern = Pattern.compile("=( )+*\\(+*@NUMBER\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern numNotEqualPattern = Pattern.compile("<>( )+*\\(+*@NUMBER\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern strLikePattern = Pattern.compile("like( )+\\(+*@LITERAL\\d{1,)", Pattern.CASE_INSENSITIVE);
	private Pattern strBetweenPattern = Pattern.compile("between( )+\\(+*@(LITERAL|NUMBER)\\d{1,}( )+and( )+@(LITERAL|NUMBER)\\d{1,)", Pattern.CASE_INSENSITIVE);
	
	public static SQLStatData groupQueryParams(String sqlText, Map<String, List<String>> args)
	{
		Collection<List<String>> listOfParams = args.values();
		for(List<String> params : listOfParams)
		{
			
		}
		return null;
	}
	
	public static String groupLessOrEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		Collections.sort(params, String.CASE_INSENSITIVE_ORDER);
		Collections.reverse(params);
		return params.get(0);
	}
	
	public static String groupGreaterOrEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		Collections.sort(params, String.CASE_INSENSITIVE_ORDER);
		return params.get(0);
	}
	
	public static String groupStrEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		String result = "in (";
		
		for(String param:params)
		{
			result += ", '" +param + "'";
		}
		result = result.replaceFirst(",", "");
		result += ")";
		
		return result;
	}
	
	public static String groupStrNotEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		String result = "not in (";
		
		for(String param:params)
		{
			result += ", '" +param + "'";
		}
		result = result.replaceFirst(",", "");
		result += ")";
		
		return result;
	}
	
	public static String groupNumEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		String result = "in (";
		
		for(String param:params)
		{
			result += ", '" +param + "'";
		}
		result = result.replaceFirst(",", "");
		result += ")";
		
		return result;
	}
	
	public static String groupNumNotEqual(List<String> params)
	{
		if(params == null)
			return null;
		
		String result = "in (";
		
		for(String param:params)
		{
			result += ", '" +param + "'";
		}
		result = result.replaceFirst(",", "");
		result += ")";
		
		return result;
	}
	
	public static String groupLike(List<String> params, String field)
	{
		if(params == null)
			return null;
		
		String result = "";
		
		for(String param:params)
		{
			result += "or " + field + " like '" +param + "'";
		}
		result = result.replaceFirst("or ", "");
		
		return result;
	}
	
	public static String groupConcat(List<String> params, List<String> field)
	{
		if(params.size()==1)
		{
			
		}
		
		return null;
	}
	
	public static String groupFuncCondition(List<String> params, List<String> field)
	{
		if(params.size()==1)
		{
			
		}
		
		return null;
	}
}
